home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 12.04 - Payroll / Payroll.java < prev    next >
Text File  |  1996-04-22  |  5KB  |  185 lines

  1. /* -----------------------------------------------------------
  2.    This illustrates a few standard classes and basic applet behavior.
  3.    Enter an employee number for an employee into a text field. 
  4.    If this employee exists, the applet will find the employee and 
  5.    display the employee's payroll information. Otherwise, the applet 
  6.    will create a new employee and add the employee to the database. 
  7.    
  8.    Java's classes: Applet     (applet)  
  9.                    TextField  (awt)     to enter new employee data
  10.                    Label      (awt)     read-only text
  11.                    GridLayout (awt)     aligns by columns and rows
  12.                    Event      (awt)     user-generated action
  13.                    Hashtable  (util)    database
  14.                    String     (lang)    text
  15.                    Integer    (lang)    number
  16.                    
  17.    Custom classes: Payroll
  18.                    Employee             payroll information
  19.                    
  20. ----------------------------------------------------------- */
  21. import java.applet.Applet;
  22. import java.awt.*;
  23. import java.util.*;
  24.  
  25. public class Payroll extends Applet {
  26.     Hashtable  db;
  27.     TextField  textFieldEmployee;
  28.     TextField  textFieldWage;
  29.     TextField  textFieldHours;
  30.     Label      labelEarned;
  31.     Employee   current;
  32.    
  33.     /* Create user interface needed by this applet. */
  34.     public void init() {
  35.     
  36.         // Create the employee database.
  37.         db = new Hashtable();
  38.         
  39.         // Arrange the user interface in a grid.
  40.         setLayout(new GridLayout(4,2)); // 4 rows, 2 columns
  41.       
  42.         // 1st row.
  43.         add(new Label("Employee number:"));
  44.         textFieldEmployee = new TextField(20); // 20 columns wide
  45.         add(textFieldEmployee);
  46.       
  47.         // 2nd row.
  48.         add(new Label("Hourly wage:"));
  49.         textFieldWage = new TextField(20); // 20 columns wide
  50.         add(textFieldWage);
  51.       
  52.         // 3rd row.
  53.         add(new Label("Hours worked:"));
  54.         textFieldHours = new TextField(20); // 20 columns wide
  55.         add(textFieldHours);
  56.       
  57.         // 4th row.
  58.         add(new Label("Earned income:"));
  59.         labelEarned = new Label(); 
  60.         add(labelEarned);
  61.             
  62.         setCurrent(null);
  63.     }
  64.     
  65.    
  66.     /** Handle events that propogate to the applet. This will include new text field data. */
  67.     public boolean action(Event e, Object arg) {
  68.         Employee employee;
  69.         int         number;
  70.             
  71.         // Create/retrieve the employee according to the number the user entered.
  72.         if (e.target == textFieldEmployee) {
  73.             
  74.             number = intFromTextField(textFieldEmployee);
  75.             employee = findEmployee(number);    
  76.                 
  77.             // Create a new employee if the employee is not already there.    
  78.             if (employee == null)                           
  79.                 employee = addNew(number);
  80.                    
  81.             // Display this employee's payroll information.       
  82.             setCurrent(employee);
  83.                     
  84.         // Set the hourly wage for the current employee.
  85.         } else if (e.target == textFieldWage) {
  86.             
  87.             if (current != null) {
  88.                 current.hourlyWage = intFromTextField(textFieldWage);
  89.                 recalcEarned();
  90.             }
  91.             
  92.         // Set the number of hours worked for the current employee.
  93.         } else if (e.target == textFieldHours) {
  94.  
  95.             if (current != null) {
  96.                 current.hoursWorked = intFromTextField(textFieldHours);
  97.                 recalcEarned();
  98.             }    
  99.         }
  100.         
  101.         return super.action(e, arg);
  102.     }
  103.     
  104.     /** This is a utility routine to retrieve an integer from a text field. */
  105.     int intFromTextField(TextField tf) {
  106.         String  s;
  107.         int     value;
  108.         
  109.         s = tf.getText();
  110.         try {
  111.            value = Integer.parseInt(s);
  112.         } catch (Exception e) {
  113.            value = 0;
  114.            setCurrent(null);
  115.         }
  116.            
  117.         return value;
  118.     }
  119.     
  120.     /** Do a database lookup using the employee's number as the key. */
  121.     Employee findEmployee(int number) {
  122.         return (Employee)db.get(new Integer(number));
  123.     }
  124.     
  125.     /** Set the text fields to display the correct information for the current employee. */
  126.     void setCurrent(Employee e) {
  127.         current = e;
  128.         
  129.         // If there isn't a current employee, initialize the fields to 0.
  130.         if (e == null) {
  131.            textFieldEmployee.setText("0");
  132.            textFieldWage.setText("0");
  133.            textFieldHours.setText("0");        
  134.         } else {
  135.             textFieldWage.setText(Integer.toString(current.hourlyWage));
  136.             textFieldHours.setText(Integer.toString(current.hoursWorked));
  137.         }
  138.         
  139.         recalcEarned();
  140.  
  141.     }
  142.     
  143.     /** Create a new employee and add it to the database */
  144.     Employee addNew(int number) {
  145.         Employee e = new Employee();
  146.         e.idNumber = number;
  147.         e.hourlyWage = 0;
  148.         e.hoursWorked = 0;
  149.         
  150.         db.put(new Integer(number), e);  // Add to the database using (key, value)
  151.         
  152.         setCurrent(e);
  153.         
  154.         return e;
  155.     }
  156.     
  157.     /** Recalculate the text to display in the "Earned income:" label. */
  158.     void recalcEarned() {
  159.         int earned;
  160.         
  161.         if (current != null) 
  162.             earned = current.earnedIncome();        
  163.         else 
  164.             earned = 0;
  165.         
  166.         labelEarned.setText(Integer.toString(earned)); 
  167.     }
  168.     
  169. }
  170.  
  171. /** Maintain payroll information for an employee. */
  172. class Employee {
  173.     int idNumber;
  174.     int hourlyWage;
  175.     int hoursWorked;
  176.     
  177.     int earnedIncome() {
  178.         return hourlyWage * hoursWorked;
  179.     }
  180. }
  181.  
  182.  
  183.  
  184.  
  185.